package com.zam.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zam.comment.Result;
import com.zam.utils.QRCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zam.service.IUserExtendService;
import com.zam.pojo.UserExtend;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/******************************
 controller层
 ******************************/
@RestController
@RequestMapping("/userExtend")
public class UserExtendController {
    @Autowired
    private IUserExtendService userExtendService;

    /*
    添加数据
    */
    @PostMapping
    public boolean save(@RequestBody UserExtend userExtend) {
        return userExtendService.saveOrUpdate(userExtend);
    }

    /*
    删除数据
    */
    @DeleteMapping("/{id}")
    public Boolean delete(@PathVariable Integer id) {
        return userExtendService.removeById(id);
    }

    /*
    删除多个数据
    */
    @DeleteMapping("/del/batch")
    public boolean deleteBatch(@RequestBody List<Integer> ids) {
        return userExtendService.removeByIds(ids);
    }

    /*
     查看全部
    */
    @GetMapping
    public List<UserExtend> findAll() {
        return userExtendService.list();
    }

    /*
    根据id查看
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        LambdaQueryWrapper<UserExtend> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserExtend::getUserId, id);
        UserExtend one = userExtendService.getOne(queryWrapper);
        return Result.success(one);
    }

    /*
    分页查询
     */
    @GetMapping("/page")
    public Page<UserExtend> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        QueryWrapper<UserExtend> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return userExtendService.page(new Page<>(pageNum, pageSize), queryWrapper);
    }

    /*
    积分充值
     */
    @GetMapping("/{id}/{money}")
    @Transactional(rollbackFor = Exception.class)
    public void update(@PathVariable("id") Integer id, @PathVariable("money") Integer money,
                       HttpServletResponse response) throws Exception {
        UserExtend one = userExtendService.getOne(new LambdaQueryWrapper<UserExtend>().eq(UserExtend::getUserId, id));
        Integer addIntegeal = 0;
        if (one.getIsVip() == 1) {
            addIntegeal = (int)(one.getIntegral() + money * 12.5);
        }else {
            addIntegeal = one.getIntegral() + money * 10;
        }

        LambdaUpdateWrapper<UserExtend> lup = new LambdaUpdateWrapper<>();
        lup.eq(UserExtend::getUserId, id);
        lup.set(UserExtend::getIntegral, addIntegeal);
        userExtendService.update(lup);
        //生成二维码
        String str = "成功充值" + money + "元，积分+" + money * 10 + "，当前积分" + addIntegeal;
        QRCode.createQRCode(str, response);
    }

    /*
    购买vip
     */
    @GetMapping("/vip/{id}")
    @Transactional(rollbackFor = Exception.class)
    public void buyVIP(@PathVariable("id") Integer id, HttpServletResponse response) throws Exception {
        UserExtend one = userExtendService.getOne(new LambdaQueryWrapper<UserExtend>().eq(UserExtend::getUserId, id));
        int addIntegeal = one.getIntegral() + 100;

        LambdaUpdateWrapper<UserExtend> lup = new LambdaUpdateWrapper<>();
        lup.eq(UserExtend::getUserId, id);
        lup.set(UserExtend::getIntegral, addIntegeal);
        lup.set(UserExtend::getIsVip, 1);
        userExtendService.update(lup);
        //生成二维码
        String str = "VIP购买成功，积分+" + 100 + "，当前积分" + addIntegeal;
        QRCode.BWQRCode(str, response);
    }
}

